

class Person {
  // 定义实例对象上属性的类型
  name: string;
  age: number;

  // 函数参数的类型
  constructor(name: string, age: number) {
    this.name = name;
    this.age = age
  }

  setName(name: string): void {
    this.name = name;
  }
}

const p = new Person('jack', 19);
// p.name;
// p.age;
// p.setName('bob')

class Son extends Person {
  // ES7
  // 静态属性给Son类使用的
  static a: number = 123;
  // 静态方法
  static say(str: string) {
    console.log(str);
  }

  // 不写static才是给实例对象使用的
  sex: string;

  constructor(name: string, age: number, sex: string) {
    super(name, age) // 调用父类的constructor方法
    this.sex = sex
  }

  address: string = '深圳'

  // 函数定义在原型对象上
  setName(name: string) {
    this.name = 'haha_' + name
  }
  // 函数定义在
  sayAge = (age: number) => {
    console.log(age);
  }
}

const s = new Son('tom', 20, '男')
console.log(s);
Son.a
Son.say('hello')


// interface Ani {
//   name: string;
//   age: number;
//   sex: string;
//   sayAge: () => void 
// }

class Animal {
  public readonly  name: string
  private readonly  age: number
  protected readonly  sex: string

  constructor(name, age, sex) {
    this.name = name;
    this.age = age;
    this.sex = sex;
  }

  sayAge() {
    console.log(this.age);
  }
}

const a: Animal = new Animal('狗狗', 2, '公')
a.name
// a.age // 报错
a.sayAge()
// a.sex

class Dog extends Animal {

  // constructor(name, age, sex) {
  //   super(name, age, sex)
  // }

  sayAge() {
    console.log(this.age); // 报错
  }
  saySex() {
    console.log(this.sex); // OK
  }
}
